//手机端发送字符串数据  on/  2号引脚置位灯亮     数据  off/  灯灭



#include "BluetoothSerial.h"

BluetoothSerial SerialBT;

void setup(){
  SerialBT.begin("first");
  Serial.println("The device started, now you can pair it with bluetooth!");
  Serial.begin(115200);
  pinMode(2, OUTPUT);
}


String recive = "";//一次所接收的字符
String data_m = "";//中间数据存储
String data = "";//最终接收完一次的数据
void loop(){

//因为数据只有8位二进制数所以一次只能传输一个字符
  if (SerialBT.available() > 0) {     //手机端发送字符串数据后面加一个   /  表示数据截止   字符串data即表示发过来的数据
      recive +=char (SerialBT.read());
      if(recive=="/"){Serial.println(data_m);data = data_m;data_m="";goto go;}
      data_m +=recive;
      go:;
      recive = "";
    }
   /*delay(50);
   Serial.println(recive);
  */

   if(data=="on"){
    digitalWrite(2,HIGH);
   }
   if(data=="off"){
    digitalWrite(2,LOW);
   }
   
  
  if (Serial.available()) {
    SerialBT.write(Serial.read());

  }
  delay(20);

}